package com.koron.order.common.service.impl;

import com.koron.common.core.util.CodeTools;
import com.koron.order.common.bean.po.OrderInstructionBean;
import com.koron.order.common.mapper.OrderInstructionMapper;
import com.koron.order.common.service.OrderInstructionService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (OrderInstruction)表服务实现类
 *
 * @author zhouj
 * @since 2021-09-14 14:41:24
 */
@Service("orderInstructionService")
public class OrderInstructionServiceImpl implements OrderInstructionService {

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, OrderInstructionBean orderInstruction) {

        OrderInstructionMapper mapper = factory.getMapper(OrderInstructionMapper.class);

        return mapper.insert(orderInstruction);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, OrderInstructionBean orderInstruction) {
        OrderInstructionMapper mapper = factory.getMapper(OrderInstructionMapper.class);
        return mapper.update(orderInstruction);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        OrderInstructionMapper mapper = factory.getMapper(OrderInstructionMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("deleteByOrderId")
    public int deleteByBizId(SessionFactory factory, String bizId, String orderType) {

        OrderInstructionMapper mapper = factory.getMapper(OrderInstructionMapper.class);
        return mapper.deleteByBizId(bizId, orderType);
    }

    @Override
    @TaskAnnotation("queryById")
    public OrderInstructionBean queryById(SessionFactory factory, String id) {

        OrderInstructionMapper mapper = factory.getMapper(OrderInstructionMapper.class);

        return mapper.queryById(id);

    }

    @Override
    @TaskAnnotation("queryList")
    public List<OrderInstructionBean> queryList(SessionFactory factory, OrderInstructionBean orderInstruction) {

        OrderInstructionMapper mapper = factory.getMapper(OrderInstructionMapper.class);

        List<OrderInstructionBean> list = mapper.queryList(orderInstruction);

        return list;
    }

    @Override
    @TaskAnnotation("queryByOrderId")
    public List<OrderInstructionBean> queryByBizId(SessionFactory factory, String bizId) {

        OrderInstructionMapper mapper = factory.getMapper(OrderInstructionMapper.class);

        List<OrderInstructionBean> list = mapper.queryByBizId(bizId);

        return list;
    }

    @Override
    public int insertBatch(SessionFactory factory, List<OrderInstructionBean> orderInstructionBeanList) {

        if (CollectionUtils.isEmpty(orderInstructionBeanList)) {
            return 0;
        }
        String bussinessId = orderInstructionBeanList.get(0).getBussinessId();
        String orderType = orderInstructionBeanList.get(0).getOrderType();

        OrderInstructionMapper mapper = factory.getMapper(OrderInstructionMapper.class);
        List<OrderInstructionBean> odList = mapper.queryByOrderId(bussinessId, orderType);
        if(CollectionUtils.isNotEmpty(odList)){
            mapper.deleteByIds(odList.stream().map(OrderInstructionBean::getId).collect(Collectors.joining(",")));
        }

        orderInstructionBeanList.forEach(p -> {
            p.setId(CodeTools.getCode32());
        });

        return mapper.insertBatch(orderInstructionBeanList);
    }

}